<template>
  <div id="live_pie">
    <div ref="pie" class="pieBox"></div>
  </div>
</template>

<script>
export default {
  props: {
    valueData: Array | Object
  },
  components: {},
  data() {
    return {};
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.drawpie();
  },

  methods: {
    drawpie() {
      let that = this;
      let num = 0;
      that.valueData.forEach(item => {
        num += item.value;
      });
      for (let i = that.valueData.length - 1; i >= 0; i--) {
        if (that.valueData[i].value / num < 0.00005) {
          that.valueData.splice(i, 1);
        }
      }
      let myChart = that.$echarts.init(that.$refs.pie);
      let option = {
        color: ['#f3ac62', '#f89448', '#f18b6a', '#f06c44', '#fd9254'],
        tooltip: {
          trigger: 'item',
          formatter: '{b} :{d}%',
          confine: true //将此限制打开后tooltip将不再溢出
        },
        series: [
          {
            // left: '20%',
            right: '10%',
            // top: '20%',
            // bottom: '20%',
            name: '访问来源',
            type: 'pie',
            radius: ['42%', '55%'],
            center: ['50%', '50%'],
            minAngle: 18,
            labelLine: {
              // 设置指示线的长度
              normal: {
                length: 12,
                length2: 8
              }
            },
            label: {
              // position: "inside",
              formatter: ' {b|{b}：}{d|{d}% }',
              borderColor: '#aaa',
              avoidLabelOverlap: true,
              rich: {
                b: {
                  fontSize: 12,
                  lineHeight: 33,
                  // color: "rgba(255,255,255,0.7)",
                  color: '#555'
                },
                d: {
                  // color: "rgba(255,255,255,0.7)",
                  align: 'center',
                  color: '#555'
                }
              }
            },
            data: that.valueData
          }
        ]
      };
      myChart.setOption(option);

      let resizeFn = function () {
        myChart.resize();
      } 
      window.addEventListener('resize',resizeFn);
      this.$once('hook:beforeDestroy', () => {
        myChart.dispose();
        myChart = null;
        window.removeEventListener('resize',resizeFn);
      });
    }
  }
};
</script>
<style lang='less' scoped>
#live_pie {
  width: 100%;
  height: 100%;
  > div {
    width: 100%;
    height: 100%;
  }
}
</style>